<template>
  <div class="user-list">
    <div class="search-box">
      <div class="search_left">
        <div class="search-box-label">
          <span>账户名</span>
          <el-input v-model="account" placeholder="请输入账户名"></el-input>
        </div>
        <div class="search-box-label">
          <span>商家</span>
          <el-input v-model="shop_name" placeholder="请输入商家名称"></el-input>
        </div>

        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </div>

      <el-button type="primary" @click="addUser">增加</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column label="编号" prop="uid" width="100"> </el-table-column>
      <el-table-column label="账号名（手机号）" prop="mobile">
      </el-table-column>
      <el-table-column label="商家" prop="shop_name"> </el-table-column>
      <el-table-column label="角色名称">
        <template slot-scope="scope">
          {{ roleData[scope.row.role_code] }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template
          slot-scope="scope"
          v-if="scope.row.role_code != 'super_admin'"
        >
          <el-button type="primary" size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChange"
      :current-page="page"
      :page-size="size"
      layout="total, prev, pager, next"
      :total="total"
      background
    ></el-pagination>

    <el-dialog
      :title="type == '1' ? '添加' : '编辑'"
      :visible.sync="dialogEdit"
      width="40%"
      @close="closeModal"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
      >
        <el-form-item label="账号名（手机号）" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="ruleForm.password" show-password @input="pwdchange($event)"></el-input>
        </el-form-item>
        <el-form-item label="商家" prop="shop_id">
          <el-select
            v-model="ruleForm.shop_id"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in shopInfo"
              :key="item.shop_id"
              :label="item.name"
              :value="item.shop_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色名称" prop="role">
          <el-radio-group v-model="ruleForm.role">
            <el-radio :label="'2'">店长</el-radio>
            <el-radio :label="'3'">店员</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogEdit = false;
            resetForm('ruleForm');
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/permission";
export default {
  name: "UserList",
  data() {
    return {
      account: "",
      shop_name: "",
      shopInfo: [],
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      roleData: {
        super_admin: "管理员",
        shop_admin: "店长",
        shop_employees: "店员",
      },
      type: "1", //1添加 2.编辑
      dialogEdit: false,
      uid: "",
      ruleForm: {
        mobile: "",
        password:"",
        shop_id: "",
        role: "2",
        name: "名称",
      },
      rules: {
        mobile: [
          {
            required: true,
            message: "请输入账户号（手机号）",
            trigger: "blur",
          },
        ],
        role: [{ required: true, message: "请选择角色", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "change" }],
        role: [{ required: true, message: "请选择角色", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getListData();
    api.getShopList({ keywords: "" }).then((res) => {
      const { list } = res.data;
      this.shopInfo = list;
    });
  },
  methods: {
    getListData() {
      let data = {
        account: this.account,
        shop_name: this.shop_name,
        page: this.page,
        size: this.size,
      };
      api.getList(data).then((res) => {
        const { list, count } = res.data;
        this.tableData = list;
        this.total = count;
      });
    },
    search() {
      this.page = 1;
      this.getListData();
    },
    handleEdit(row) {
      this.ruleForm = { ...row };
      this.dialogEdit = true;
      this.type = "2";
      const { mobile,passwd, uid, shop_id, role_code } = row;
      this.ruleForm.mobile = mobile;
      this.ruleForm.password = passwd;
      this.ruleForm.shop_id = shop_id;
      this.uid = uid;
      this.ruleForm.role = role_code == "shop_admin" ? "2" : "3";
    },
    addUser() {
      this.dialogEdit = true;
      this.type = "1";
      this.uid = "";
      this.ruleForm.mobile = "";
      this.ruleForm.password = "123456";
      this.ruleForm.shop_id = "";
      this.ruleForm.role = "2";
    },
    handleDelete(row) {
      this.uid = row.uid;
      this.$confirm("确定要删除此用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api
            .delMember({ uid: this.uid })
            .then((res) => {
              if (res.code == 200200) {
                this.$message.success(res.msg);
                this.getListData();
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.uid = "";
        });
    },
    resetSearch() {
      this.account = "";
      this.shop_name = "";
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = { ...this.ruleForm };
          let role_code = data.role == "2" ? "shop_admin" : "shop_employees";
          data.role_code = role_code;
          let fn =
            this.type == "1"
              ? api.createMember(data)
              : api.editMember({ ...data, uid: this.uid });
          fn.then((res) => {
            if (res.code == 200200) {
              this.Message.success(res.msg);
              this.dialogEdit = false;
              this.getListData();
            } else {
              this.Message.error(res.msg);
            }
          }).catch((err) => {
            this.Message.error(err.msg);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
    },
    closeModal() {
      this.$nextTick(() => {
        this.$refs["ruleForm"].clearValidate();
      });
    },
    currentChange(val) {
      this.page = val;
      this.getListData();
    },
    pwdchange(e){
      this.$forceUpdate();
    }

  },
};
</script>

<style lang="scss" scoped>
.user-list {
  padding: 40px;
  .search-box {
    display: flex;
    justify-content: space-between;
    .search_left {
      display: flex;
    }
    .el-button {
      height: 40px;
    }
    .el-input {
      width: 220px;
      margin-right: 15px;
    }
    .search-box-label {
      box-sizing: border-box;
      font-size: 14px;
      margin-bottom: 20px;
      color: #333;
      span {
        margin-right: 10px;
        display: inline-block;
      }
      .filter-item {
        width: 200px;
      }
    }
  }
}
</style>
